define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'record/index' + location.search,
                    add_url: 'record/add',
                    edit_url: 'record/edit',
                    del_url: 'record/del',
                    multi_url: 'record/multi',
                    import_url: 'record/import',
                    table: 'record',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                escape:false,
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'title', title: __('Title')},
                        {field: 'id',visible:false, title: __('Id')},
                        {field: 'admin_id',visible:false, title: __('添加人'),searchList: $.getJSON("auth/admin/searchList"), operate: '=',formatter: function (value,row) {
                            return row.admin_text;
                        }},

                        {field: 'plan_id', title: __('Plan_id'),searchList: $.getJSON("plan/searchList?type=record"),operate: '=',
                            formatter: function (value,row) {
                            return row.plan_id_text;
                        }},
                        {field: 'remarks', title: __('Remarks'), operate: 'LIKE',
                        formatter: function(value,row){
                            if(value.length > 10){
                                return row.remarks_text.toString().substr(0, 10) + '...';
                            }else{
                                return row.remarks_text;
                            }
                        }},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'weigh',visible:false, title: __('Weigh'), operate: false},
                        {field: 'state',visible:false, title: __('State'), searchList: {"0":__('State 0'),"1":__('State 1')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'),
                            buttons: [
                                {name: '详情', text: __('详情'), title: __('详情'), extend: 'data-area= \'["70%", "60%"]\'', icon: 'fa fa-list', classname: 'btn btn-xs btn-success btn-dialog', url: 'record/detail'},
                            ],
                            table: table, events: Table.api.events.operate,
                            formatter: function(value,row,index){
                                var that = $.extend({},this);
                                var table = $(that.table).clone(true);
                                // $(table).data("operate-edit",null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that,value,row,index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'record/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'record/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'record/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            require(['laohan/onlylayui']);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"), function(data, ret){
                    if(ret.msg !=''){
                        top.window.$('[addtabs="341"]').trigger("click");
                        top.Toastr.success(ret.msg);
                        parent.location.href = ret.url;
                    }
                })
            }
        }
    };
    return Controller;
});
